<?

class AccountController extends Controller {
    
	public $js = array("account");
	
	public function mainAction(){
		
		$connect = $this->connections;
		$user = $connect->fetchModels("Users",array("username"=>$_SESSION["username"]));
		$config = $connect->fetchModels("User_prefs",array("user_id "=>$_SESSION["user_id"]));
		
		if(isset($_POST["envoyerLogo"])){
			$chemin_destination = PUBLIC_DIR.'/logos/'.$_SESSION["user_id"].'/';    
			if(!file_exists($chemin_destination)){
				mkdir($chemin_destination);
			}
			move_uploaded_file($_FILES['logo']['tmp_name'], $chemin_destination.$_FILES['logo']['name']);    
			$config->entete = $_FILES['logo']['name'];
			$connect->save($config);
		}
		$vars = array(
			"user"=>$user,
			"config"=>$config
		);
		return $this->render("account/infos",$vars);
	}
	public function updateAccountAction(){
		$connect = $this->connections;
		$user = $connect->fetchModels("Users",array("username"=>$_SESSION["username"]));
		
		foreach($_POST as $var=>$val){
			$user->$var = $val;
		}
		$connect->save($user);
		
		return json_encode(array("response_code"=>"ok"));
	}
	public function updateConfigAction(){
		$connect = $this->connections;
		$config = $connect->fetchModels("User_prefs",array("user_id "=>$_SESSION["user_id"]));
		
		foreach($_POST as $var=>$val){
			$config->$var = $val;
		}
		$connect->save($config);
		
		return json_encode(array("response_code"=>"ok"));
	}
}
?>